/*     */ package com.zimbra.cs.account.callback;
/*     */ 
/*     */ import com.google.common.collect.BiMap;
/*     */ import com.google.common.collect.HashBiMap;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.StringUtil;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import com.zimbra.cs.account.AttributeCallback;
/*     */ import com.zimbra.cs.account.AttributeCallback.SingleValueMod;
/*     */ import com.zimbra.cs.account.AttributeManager;
/*     */ import com.zimbra.cs.account.Config;
/*     */ import com.zimbra.cs.account.Entry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import java.util.HashMap;
/*     */ import java.util.HashSet;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import java.util.Map.Entry;
/*     */ import java.util.Set;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CheckPortConflict
/*     */   extends AttributeCallback
/*     */ {
/*     */   private static final Set<String> ProxyPortAttrs;
/*     */   private static final Set<String> NonProxyPortAttrs;
/*     */   
/*     */   static
/*     */   {
/*  39 */     ProxyPortAttrs = new HashSet();
/*     */     
/*     */ 
/*     */ 
/*  43 */     ProxyPortAttrs.add("zimbraAdminProxyPort");
/*     */     
/*  45 */     ProxyPortAttrs.add("zimbraImapProxyBindPort");
/*  46 */     ProxyPortAttrs.add("zimbraImapSSLProxyBindPort");
/*     */     
/*  48 */     ProxyPortAttrs.add("zimbraPop3ProxyBindPort");
/*  49 */     ProxyPortAttrs.add("zimbraPop3SSLProxyBindPort");
/*     */     
/*  51 */     ProxyPortAttrs.add("zimbraMailProxyPort");
/*  52 */     ProxyPortAttrs.add("zimbraMailSSLProxyPort");
/*  53 */     ProxyPortAttrs.add("zimbraMailSSLProxyClientCertPort");
/*     */     
/*     */ 
/*  56 */     NonProxyPortAttrs = new HashSet();
/*     */     
/*     */ 
/*     */ 
/*  60 */     NonProxyPortAttrs.add("zimbraAdminPort");
/*     */     
/*  62 */     NonProxyPortAttrs.add("zimbraImapBindPort");
/*  63 */     NonProxyPortAttrs.add("zimbraImapSSLBindPort");
/*     */     
/*  65 */     NonProxyPortAttrs.add("zimbraPop3BindPort");
/*  66 */     NonProxyPortAttrs.add("zimbraPop3SSLBindPort");
/*     */     
/*  68 */     NonProxyPortAttrs.add("zimbraMailPort");
/*  69 */     NonProxyPortAttrs.add("zimbraMailSSLPort");
/*  70 */     NonProxyPortAttrs.add("zimbraMailSSLClientCertPort");
/*     */     
/*  72 */     NonProxyPortAttrs.add("zimbraLmtpBindPort");
/*  73 */     NonProxyPortAttrs.add("zimbraRemoteManagementPort");
/*  74 */     NonProxyPortAttrs.add("zimbraMemcachedBindPort");
/*  75 */     NonProxyPortAttrs.add("zimbraMessageChannelPort");
/*     */   }
/*     */   
/*  78 */   private static Set<String> sPortAttrs = new HashSet(NonProxyPortAttrs);
/*     */   
/*     */ 
/*     */ 
/*     */   public void preModify(CallbackContext context, String attrName, Object attrValue, Map attrsToModify, Entry entry)
/*     */     throws ServiceException
/*     */   {
/*  85 */     if ((entry != null) && (!(entry instanceof Server)) && (!(entry instanceof Config))) { return;
/*     */     }
/*  87 */     if (context.isDoneAndSetIfNot(CheckPortConflict.class)) {
/*  88 */       return;
/*     */     }
/*     */     
/*     */ 
/*  92 */     sPortAttrs.addAll(ProxyPortAttrs);
/*     */     
/*     */ 
/*  95 */     if ((!sPortAttrs.contains(attrName)) || (!AttributeManager.getInstance().isServerInherited(attrName)))
/*     */     {
/*  97 */       if (!$assertionsDisabled) { throw new AssertionError();
/*     */       }
/*     */     }
/* 100 */     if ((entry == null) || ((entry instanceof Server))) {
/* 101 */       checkServer((Server)entry, attrsToModify);
/*     */     } else
/* 103 */       checkConfig((Config)entry, attrsToModify);
/*     */   }
/*     */   
/*     */   private void checkServer(Server server, Map<String, Object> serverAttrsToModify) throws ServiceException {
/* 107 */     Map<String, String> ports = new HashMap();
/* 108 */     Map<String, Object> defaults = Provisioning.getInstance().getConfig().getServerDefaults();
/*     */     
/*     */ 
/* 111 */     if (server != null) {
/* 112 */       for (String attrName : sPortAttrs) {
/* 113 */         if (!serverAttrsToModify.containsKey(attrName)) {
/* 114 */           ports.put(server.getAttr(attrName), attrName);
/*     */         }
/*     */       }
/*     */     }
/*     */     
/* 119 */     for (Map.Entry<String, Object> attrToModify : serverAttrsToModify.entrySet()) {
/* 120 */       String attrName = (String)attrToModify.getKey();
/*     */       
/* 122 */       if (sPortAttrs.contains(attrName))
/*     */       {
/*     */ 
/* 125 */         AttributeCallback.SingleValueMod mod = singleValueMod(serverAttrsToModify, attrName);
/* 126 */         String newValue = null;
/* 127 */         if (mod.setting()) {
/* 128 */           newValue = mod.value();
/*     */         }
/*     */         else {
/* 131 */           Object defValue = defaults.get(attrName);
/* 132 */           if (defValue != null) {
/* 133 */             if ((defValue instanceof String)) {
/* 134 */               newValue = (String)defValue;
/*     */             } else {
/* 136 */               ZimbraLog.misc.info("default value for " + attrName + " should be a single-valued attribute, invalid default value ignored");
/*     */             }
/*     */           }
/*     */         }
/* 140 */         if (conflict(server, ports, newValue, attrName)) {
/* 141 */           String serverInfo = " on server " + server.getName();
/* 142 */           throw ServiceException.INVALID_REQUEST("port " + newValue + " conflict between " + attrName + " and " + (String)ports.get(newValue) + serverInfo, null);
/*     */         }
/*     */         
/* 145 */         ports.put(newValue, attrName);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/* 150 */   private void checkConfig(Config config, Map<String, Object> configAttrsToModify) throws ServiceException { BiMap<String, String> newDefaults = HashBiMap.create();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 156 */     for (String attrName : sPortAttrs) {
/* 157 */       if (!configAttrsToModify.containsKey(attrName)) {
/* 158 */         newDefaults.put(config.getAttr(attrName), attrName);
/*     */       }
/*     */     }
/*     */     
/* 162 */     for (Map.Entry<String, Object> attrToModify : configAttrsToModify.entrySet()) {
/* 163 */       String attrName = (String)attrToModify.getKey();
/*     */       
/* 165 */       if (sPortAttrs.contains(attrName))
/*     */       {
/*     */ 
/* 168 */         AttributeCallback.SingleValueMod mod = singleValueMod(configAttrsToModify, attrName);
/* 169 */         String newValue = null;
/* 170 */         if (mod.setting()) {
/* 171 */           newValue = mod.value();
/*     */         }
/* 173 */         if (conflict(null, newDefaults, newValue, attrName)) {
/* 174 */           throw ServiceException.INVALID_REQUEST("port " + newValue + " conflict between " + attrName + " and " + (String)newDefaults.get(newValue) + " on global config", null);
/*     */         }
/*     */         
/* 177 */         newDefaults.put(newValue, attrName);
/*     */       }
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 184 */     List<Server> servers = Provisioning.getInstance().getAllServers();
/* 185 */     for (Server server : servers) {
/* 186 */       checkServerWithNewDefaults(server, newDefaults, configAttrsToModify);
/*     */     }
/*     */   }
/*     */   
/*     */   private void checkServerWithNewDefaults(Server server, BiMap<String, String> newDefaults, Map<String, Object> configAttrsToModify)
/*     */     throws ServiceException
/*     */   {
/* 193 */     Map<String, String> ports = new HashMap();
/*     */     
/* 195 */     for (String attrName : sPortAttrs) {
/* 196 */       String newValue = null;
/* 197 */       String curValue = server.getAttr(attrName, false);
/* 198 */       if (curValue == null) {
/* 199 */         newValue = (String)newDefaults.inverse().get(attrName);
/*     */       } else {
/* 201 */         newValue = curValue;
/*     */       }
/* 203 */       if (conflict(server, ports, newValue, attrName)) {
/* 204 */         String conflictWith = (String)ports.get(newValue);
/*     */         
/* 206 */         if ((configAttrsToModify.containsKey(attrName)) || (configAttrsToModify.containsKey(conflictWith))) {
/* 207 */           throw ServiceException.INVALID_REQUEST("port " + newValue + " conflict between " + attrName + " and " + (String)ports.get(newValue) + " on server " + server.getName(), null);
/*     */         }
/*     */       } else {
/* 210 */         ports.put(newValue, attrName);
/*     */       }
/*     */     }
/*     */   }
/*     */   
/* 215 */   private boolean conflict(Server server, Map<String, String> ports, String port, String attrName) { if (StringUtil.isNullOrEmpty(port))
/* 216 */       return false;
/* 217 */     if (port.equals("0"))
/* 218 */       return false;
/* 219 */     if ((server != null) && 
/* 220 */       ((!server.hasMailboxService()) || (!server.hasProxyService())) && (
/* 221 */       ((ProxyPortAttrs.contains(attrName)) && (NonProxyPortAttrs.contains(ports.get(port)))) || ((NonProxyPortAttrs.contains(attrName)) && (ProxyPortAttrs.contains(ports.get(port))))))
/*     */     {
/* 223 */       return false;
/*     */     }
/*     */     
/*     */ 
/* 227 */     return ports.containsKey(port);
/*     */   }
/*     */   
/*     */   public void postModify(CallbackContext context, String attrName, Entry entry) {}
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/account/callback/CheckPortConflict.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */